//
//  NewsViewController.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "NewsViewController.h"
#import "RequestURl.h"
#import "CommonModel.h"
#import "PicModel.h"
#import "CommonCell.h"

@interface NewsViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)NSMutableArray *arrayAllData;
@end

@implementation NewsViewController


- (void)loadView {
    
    
    self.sortArray = [NSMutableArray arrayWithObjects:@"头条",@"苹果",@"新闻",@"手机",@"评测",@"视频",@"企业",@"游戏", nil];
    self.buttonWidth = 48;
    self.newsV = [[NewsView alloc] initWithFrame:[UIScreen mainScreen].bounds sortArray:self.sortArray buttonWidth:self.buttonWidth] ;
    
    self.view = self.newsV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    self.newsV.sortSV.delegate = self;
    self.newsV.showView.delegate = self;
    
    NSInteger i = 110;
    for (UIButton *button in self.newsV.sortSV.subviews) {
        if ([button isKindOfClass:[UIButton class]] == YES) {
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            button.tag = i++;
        }
        if (i == 110) {
            [self buttonAction:button];
        }
    }
    
    
    for (UITableView *tableView in self.newsV.showView.subviews) {
        if ([tableView isKindOfClass:[UITableView class]] == YES) {
            tableView.delegate = self;
            tableView.dataSource = self;
        }
    }
    
    RequestURl *request = [[RequestURl alloc] init];
    [request requestWithUrlID:0 UrlPage:0 success:^(id item) {
        self.arrayAllData = item;
        NSLog(@"%@",self.arrayAllData);
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

//tableview代理
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}




//分类切换按钮响应事件
- (void)buttonAction:(UIButton *)sender {
    NSInteger offset = sender.tag - 110;
    [UIView animateWithDuration:1 animations:^{
        if (offset < self.sortArray.count - 2 && offset > 2) {
            self.newsV.sortSV.contentOffset = CGPointMake((offset - 3) * self.buttonWidth, 0);
        } else if(offset < 3) {
            self.newsV.sortSV.contentOffset = CGPointMake(0, 0);
        } else {
            self.newsV.sortSV.contentOffset = CGPointMake((self.sortArray.count - offset) * self.buttonWidth, 0);
        }
        
        self.newsV.showView.contentOffset = CGPointMake(offset * kScreenW, 0);
    }];
    
}

//scrollView代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.tag == 101) {
        return;
    }
    
    CGFloat offset = self.newsV.showView.contentOffset.x;
    CGFloat scale = offset / kScreenW;
    offset = self.buttonWidth * scale;
    self.newsV.sortSV.contentOffset = CGPointMake(offset, 0);
    
}

- (NSMutableArray *)arrayAllData {
    if (!_arrayAllData) {
        _arrayAllData = [NSMutableArray array];
    }
    return _arrayAllData;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
